/** index.js By Beginner Emain:zheng_jinfan@126.com HomePage:http://www.zhengjinfan.cn */

var tab;

layui.config({
	base: 'js/',
	version:new Date().getTime()
}).use(['element', 'layer', 'navbar', 'tab', 'form'], function() {
	var element = layui.element(),
		$ = layui.jquery,
		layer = layui.layer,
		navbar = layui.navbar();
		layerTips = parent.layer === undefined ? layui.layer : parent.layer, //获取父窗口的layer对象
		tab = layui.tab({
			elem: '.admin-nav-card' //设置选项卡容器
				,
			maxSetting: {
				max: 10,
				tipMsg: '只能开10个哇，不能再开了。真的。'
			},
			autoRefresh: true,
			contextMenu:true
		});

		
		//全局的AJAX访问，处理AJAX清求时SESSION超时
		$.ajaxSetup({
		    contentType:"application/x-www-form-urlencoded;charset=utf-8",
		    complete:function(XMLHttpRequest,textStatus){
		          //通过XMLHttpRequest取得响应头，sessionstatus           
		          var sessionstatus=XMLHttpRequest.getResponseHeader("sessionstatus"); 
		          loginUrl = XMLHttpRequest.getResponseHeader("location"); 
		          if(sessionstatus=='00004'){
		               //这里怎么处理在你，这里跳转的登录页面
		        	 // if( window.top != window.self ){
		  				window.top.location = loginUrl;
		  			//}
		       }
		    }
		});

	//iframe自适应
	$(window).on('resize', function() {
		var $content = $('.admin-nav-card .layui-tab-content');
		$content.height($(this).height() - 147);
		$content.find('iframe').each(function() {
			$(this).height($content.height());
		});
	}).resize();
	var  nav_data;
	if(userright=='14'){
		nav_data = navs;
	}else{
		nav_data = navs_user
	}
	//设置navbar
	navbar.set({
		spreadOne: false,
		elem: '#admin-navbar-side',
		cached: true,
		data: nav_data
		/*cached:true,
		url: 'datas/nav.json'*/
	});
	//渲染navbar
	navbar.render();
	//监听点击事件
	navbar.on('click(side)', function(data) {
		tab.tabAdd(data.field);
	     //删除指定Tab项
		//tab.tabDelete(data.field); //删除
	});

	$('.admin-side-toggle').on('click', function() {
		var sideWidth = $('#admin-side').width();
		if(sideWidth === 200) {
			$('#admin-body').animate({
				left: '0'
			}); //admin-footer
			$('#admin-footer').animate({
				left: '0'
			});
			$('#admin-side').animate({
				width: '0'
			});
		} else {
			$('#admin-body').animate({
				left: '200px'
			});
			$('#admin-footer').animate({
				left: '200px'
			});
			$('#admin-side').animate({
				width: '200px'
			});
		}
	});
	$('.admin-side-full').on('click', function() {
		var docElm = document.documentElement;
		//W3C  
		if(docElm.requestFullscreen) {
			docElm.requestFullscreen();
		}
		//FireFox  
		else if(docElm.mozRequestFullScreen) {
			docElm.mozRequestFullScreen();
		}
		//Chrome等  
		else if(docElm.webkitRequestFullScreen) {
			docElm.webkitRequestFullScreen();
		}
		//IE11
		else if(elem.msRequestFullscreen) {
			elem.msRequestFullscreen();
		}
		layer.msg('按Esc即可退出全屏');
	});
	
	

	//锁屏
	$(document).on('keydown', function() {
		var e = window.event;
		if(e.keyCode === 76 && e.altKey) {
			//alert("你按下了alt+l");
			lock($, layer);
		}
	});
	$('#lock').on('click', function() {
		lock($, layer);
	});

	//手机设备的简单适配
	var treeMobile = $('.site-tree-mobile'),
		shadeMobile = $('.site-mobile-shade');
	treeMobile.on('click', function() {
		$('body').addClass('site-mobile');
	});
	shadeMobile.on('click', function() {
		$('body').removeClass('site-mobile');
	});
	
	//修改密码
	$("#edit-pwd").on('click', function(){
		$.get('temp/edit-pwd.html', null, function(form) {
			 layer.open({
				type: 1,
				title: '修改密码',
				content: form,
				btn: ['提交', '取消'],
				shade: false,
				offset: ['100px', '30%'],
				area: ['600px', '400px'],
				zIndex: 19950924,
				yes: function(index) {
					//触发表单的提交事件
					//设置提交地址
					$('form.layui-form').find('button[lay-filter=edit]').click();
				},
				success: function(layero, index){
					var form = layui.form();
					form.render();
					
					form.on('submit(edit)', function(data) {
						$.ajax({
							type: 'post',
							url:  'user/editpsd',
							data: data.field,
							dataType: 'json',
							success: function(result, status, xhr) {
								if(result.flag){
									layer.close(index);
									}
								layerTips.msg(result.msg);
								}
						});
						
						return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。									
					});
					
					}
			});
		});
	});
	
	  var myChart1 = echarts.init(document.getElementById('avgsex'));
      // 指定图表的配置项和数据
      var option1 = {
          title: {
              text: '频道数据统计'
          },
          toolbox: { //可视化的工具箱
              show: true,
              feature: {
                  dataView: { //数据视图
                      show: true
                  },
                  restore: { //重置
                      show: true
                  },
                  dataZoom: { //数据缩放视图
                      show: true
                  },
                  saveAsImage: {//保存图片
                      show: true
                  },
                  magicType: {//动态类型切换
                      type: ['bar', 'line']
                  }
              }
          },
          tooltip: { trigger: 'axis'},
          legend: {
              data:['RSS数量']
          },
          calculable : true,
          grid: {  
              y2: 140  
          },
          xAxis: {
        	  axisLabel:{  
                  interval:0,//横轴信息全部显示  
                  rotate:0,//-30度角倾斜显示  
             }  
          },
          yAxis: [
        	  {
                  type : 'value'
              }
          ],
          series: [{
              name: '销量',
              type: 'bar',
              data: [5, 20, 36, 10, 10, 20],
          }]
      };
     function loadbar(){
  	  myChart1.clear();  
      myChart1.showLoading({text: '正在努力的读取数据中...'});  
      $.ajax({type: 'get',url:  'rsscount',
			//data: data.field,
			async: false,dataType: 'json',success: function(result, status, xhr) {
          if (result.isOk) {  
        	  option1.xAxis.data = result.xAxis;
        	  option1.series[0].type = "bar";
        	  option1.series[0].name = "RSS数量";
        	  option1.series[0].data = result.sdata1;
          	myChart1.setOption(option1,true);  
          	myChart1.hideLoading();  
          } else {  
              alert('提示', data.msg);  
          }  
      }
      });  
      }
      loadbar();
    
});

var isShowLock = false;
function lock($, layer) {
	if(isShowLock)
		return;
	//自定页
	layer.open({
		title: false,
		type: 1,
		closeBtn: 0,
		anim: 6,
		content: $('#lock-temp').html(),
		shade: [0.9, '#393D49'],
		success: function(layero, lockIndex) {
			isShowLock = true;
			//给显示用户名赋值
			layero.find('input[name=lockPwd]').on('focus', function() {
					var $this = $(this);
					if($this.val() === '输入密码解锁..') {
						$this.val('').attr('type', 'password');
					}
				})
				.on('blur', function() {
					var $this = $(this);
					if($this.val() === '' || $this.length === 0) {
						$this.attr('type', 'text').val('输入密码解锁..');
					}
				});
			//在此处可以写一个请求到服务端删除相关身份认证，因为考虑到如果浏览器被强制刷新的时候，身份验证还存在的情况			
			//do something...
			//e.g. 
			/*
			 $.post(url,params,callback,'json');
			 */
			//绑定解锁按钮的点击事件
			layero.find('button#unlock').on('click', function() {
				var $lockBox = $('div#lock-box');
				var userName = $lockBox.find('div#lockUserName').text();
				var pwd = $lockBox.find('input[name=lockPwd]').val();
				if(pwd === '输入密码解锁..' || pwd.length === 0) {
					layer.msg('请输入密码..', {
						icon: 2,
						time: 1000
					});
					return;
				}
				unlock(userName, pwd);
			});
			/**
			 * 解锁操作方法
			 * @param {String} 用户名
			 * @param {String} 密码
			 */
			var unlock = function(un, pwd) {
				//这里可以使用ajax方法解锁
				$.post('user/unlock',{username:un,password:pwd},function(data){
				 	//验证成功
					if(data.isOk){
						//关闭锁屏层
						layer.msg(data.msg,{icon:3,time:1000});
						isShowLock = false;
						layer.close(lockIndex);
					}else{
						layer.msg(data.msg,{icon:2,time:1000});
					}					
				},'json');
				
				//isShowLock = false;
				//演示：默认输入密码都算成功
				//关闭锁屏层
				//layer.close(lockIndex);
			};
		}
	});
};